%% @doc templ_dict のテスト
%%
-module(t_templ_dict).
-import(templ_dict, [init/1,get_context/0,set_context/1,put_value/2,
					 set_delim/2,get_ldelim/0,get_rdelim/0,
					 append_text/1,append_if/1,append_else/0,append_let/2,append_foreach/2,
					 pop_stmt/0,pop_if_stmt/0,pop_let_stmt/0,pop_foreach_stmt/0,
					 get_stmt/0]).

-include_lib("eunit/include/eunit.hrl").

context_test() ->
	init([]),
	put_value("foo", "bar"),
	Ctx = get_context(),
	?assertMatch({ok,"bar"}, dict:find("foo", Ctx)),
	?assertError({keyexists,"foo"}, put_value("foo", "hoge")).

init_test() ->
	init([{"hoge", abc}, {"foo", "bar"}]),
	Ctx = get_context(),
	?assertMatch({ok,"bar"}, dict:find("foo", Ctx)),
	?assertMatch({ok,abc}, dict:find("hoge", Ctx)),
	set_context([{"hoge", 123}]),
	Ctx1 = get_context(),
	?assertMatch({ok,123}, dict:find("hoge", Ctx1)),
	?assertMatch(error, dict:find("foo", Ctx1)).

delim_test() ->
	init([]),
	?assertMatch("{", get_ldelim()),
	?assertMatch("}", get_rdelim()),
	set_delim("---", "==="),
	?assertMatch("---", get_ldelim()),
	?assertMatch("===", get_rdelim()),
	set_delim("foo", "bar"),
	?assertMatch("foo", get_ldelim()),
	?assertMatch("bar", get_rdelim()).

expr_test() ->
	init([]),
	append_text("hoge"),
	append_if({expr, true}),
	append_text("foo"),
	append_else(),
	append_text("bar"),
	pop_stmt(),
	?assertMatch([[{ok,"hoge"}, {if_,_}, [{ok,"foo"}], elseif, [{ok,"bar"}]]], get_stmt()).

expr1_test() ->
	init([]),
	append_if({expr, true}),
	append_text("foo"),
	append_else(),
	append_text("bar"),
	pop_if_stmt(),
	?assertMatch([[{if_,_}, [{ok,"foo"}], elseif, [{ok,"bar"}]]], get_stmt()).

expr2_test() ->
	init([]),
	append_let({expr, "a"}, {expr, 123}),
	append_text("foo"),
	append_foreach({expr, "b"}, {expr, 456}),
	append_text("hoge"),
	append_else(),
	append_text("moge"),
	pop_foreach_stmt(),
	append_text("hogehoge"),
	append_else(),
	append_text("bar"),
	pop_let_stmt(),
	?assertMatch([[{let_,_,_}, [{ok,"foo"}, {foreach,_,_}, [{ok,"hoge"}], elseforeach, [{ok,"moge"}], {ok,"hogehoge"}], elselet, [{ok,"bar"}]]], get_stmt()).

expr3_test() ->
	init([]),
	append_if({expr, true}),
	append_else(),
	?assertError({wrongsyntax,_}, pop_let_stmt()),
	pop_if_stmt(),
	?assertMatch([[{if_,_}, [], elseif, []]], get_stmt()).

expr4_test() ->
	init([]),
	append_if({expr, true}),
	append_let({expr, "a"}, {expr, 1}),
	?assertError({wrongsyntax,_}, pop_if_stmt()),
	pop_let_stmt(),
	pop_if_stmt(),
	?assertMatch([[{if_,_}, [{let_,_,_}, []]]], get_stmt()).
